package task.manager;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * Activity class for a task.
 * 
 * @author Harrison Saylor, Aaron Mead, Marc Huet, Greg Ruskin
 * @version 2
 */
public class TaskActivity extends Activity{
	
	private Button backBtn;
	private TextView name;
	private CheckBox completed;
	private TextView time;
	private TextView date;
	private TextView address;
	private Button toMap;
	
	private Task task;
	
	private UserDataFacade userDataFacade;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewtaskscreen);
		
		userDataFacade = UserDataFacade.getInstance();
		
		backBtn = (Button) findViewById(R.id.button1);
		name = (TextView) findViewById(R.id.textView1);
		completed = (CheckBox) findViewById(R.id.checkBox1);
		time = (TextView) findViewById(R.id.textView2);
		date = (TextView) findViewById(R.id.textView3);
		address = (TextView) findViewById(R.id.textView4);
		toMap = (Button) findViewById(R.id.button2);
		
		task = userDataFacade.getActiveTask();
		
		name.setText(task.getTitle());
		completed.setChecked(task.getIsCompleted());
		time.setText(task.getTimeString());
		date.setText(task.getDateString());
		address.setText(task.getAddress());
		
		completed.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			//@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				task.setIsCompleted(isChecked);
				
			}
			
			
		});
		
		backBtn.setOnClickListener(new OnClickListener(){

			//@Override
			public void onClick(View v) {
				try {
					userDataFacade.saveTaskData();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
					Intent nextScreen = new Intent(getApplicationContext(), TaskViewActivity.class);
	        		startActivity(nextScreen);
	        		finish();
			}
			
			
		});
		
		toMap.setOnClickListener(new OnClickListener(){

			//@Override
			public void onClick(View v) {
				
					Intent nextScreen = new Intent(getApplicationContext(), GoogleMapsView.class);
	        		startActivity(nextScreen);
	        		finish();
			}
			
			
		});
		
		
	}

}
